<?php

/**
 * myDoctor form.
 *
 * @package    docplus.co.za
 * @subpackage form
 * @author     Fox Matuku
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class myPracticeDetailsForm extends BaseDoctorForm {

	public function configure() {
		unset(
			$this['created_at'], $this['updated_at']
		);

		$this->widgetSchema['user_id'] = new sfWidgetFormInputHidden();
		$this->widgetSchema['gender'] = new sfWidgetFormInputHidden();
		$this->widgetSchema['language_id'] = new sfWidgetFormInputHidden();
		$this->widgetSchema['work_phone'] = new sfWidgetFormInputText();
		$this->widgetSchema['mobile_phone'] = new sfWidgetFormInputText();
		$this->widgetSchema['street_address'] = new sfWidgetFormInputText();
		$this->widgetSchema['post_code'] = new sfWidgetFormInputText();
		$this->widgetSchema['area'] = new sfWidgetFormInputText();
		$this->widgetSchema['city_id'] = new sfWidgetFormDoctrineChoice(array('model' => 'City', 'add_empty' => false));
		$this->widgetSchema['region_id'] = new sfWidgetFormDoctrineChoice(array('model' => 'Region', 'add_empty' => false));
		$this->widgetSchema['speciality_id']->setOption("add_empty", "Please select a speciality");
		$this->widgetSchema["speciality_id"]->setAttribute("class", "select-box-small");
		$this->widgetSchema['city_id']->setOption("add_empty", "Please select a city");
		$this->widgetSchema["city_id"]->setAttribute("class", "select-box-small");
		$this->widgetSchema['region_id']->setOption("add_empty", "Please select a region");
		$this->widgetSchema["region_id"]->setAttribute("class", "select-box-small");

		$this->validatorSchema['user_id'] = new sfValidatorString(array('required' => false));
		$this->validatorSchema['practice_name'] = new sfValidatorString(array('max_length' => 255, 'required' => true), array('required' => 'Your *practice name* is required.'));
		$this->validatorSchema['practice_number'] = new sfValidatorString(array('max_length' => 255, 'required' => false));
		$this->validatorSchema['professional_statement'] = new sfValidatorString(array('max_length' => 5000, 'required' => true), array('required' => 'Your *professional statement* is required.'));
		$this->validatorSchema['work_phone'] = new gfPhoneNumberValidator(array("required" => true), array("invalid" => "Your *work phone* is invalid.", "required" => "Your *work phone* is required."));
		$this->validatorSchema['mobile_phone'] = new gfPhoneNumberValidator(array("required" => true), array("invalid" => "Your *mobile phone* is invalid.", "required" => "Your *mobile phone* is required."));
		$this->validatorSchema['street_address'] = new sfValidatorString(array('max_length' => 255, 'required' => true), array('required' => 'Your *street address* is required.'));
		$this->validatorSchema['post_code'] = new sfValidatorString(array('max_length' => 255, 'required' => true), array('required' => 'Your *post code* is required.'));
		$this->validatorSchema['area'] = new sfValidatorString(array('max_length' => 255, 'required' => true), array('required' => 'Your *area* is required.'));
		$this->validatorSchema['city_id'] = new sfValidatorString(array('max_length' => 255, 'required' => true), array('required' => 'Your *city* is required.'));
		$this->validatorSchema['region_id'] = new sfValidatorString(array('max_length' => 255, 'required' => true), array('required' => 'Your *region* is required.'));

		$this->setContactDefaults();
	}

	/**
	 * Set the contact fields
	 */
	public function setContactDefaults() {
		$contact = $this->getOption("contact");
		if (is_object($contact)) {
			$this->setDefaults(array(
				"work_phone" => $contact->getWorkPhone(),
				"mobile_phone" => $contact->getMobilePhone(),
				"street_address" => $contact->getStreetAddress(),
				"post_code" => $contact->getPostCode(),
				"area" => $contact->getArea(),
				"city_id" => $contact->getCityId(),
				"region_id" => $contact->getRegionId()
			));
		}
	}

	/**
	 * Override the save method to save the merged contact form.
	 */
	public function save($con = null) {
		$this->updateContact();
		return parent::save($con);
	}

	/**
	 * Updates the contact merged form
	 */
	protected function updateContact() {
		$contact = ContactTable::getInstance()->findOneByUserId($this->values['user_id']);
		if (is_object($contact)) {
			$contact->setWorkPhone($this->values['work_phone']);
			$contact->setMobilePhone($this->values['mobile_phone']);
			$contact->setStreetAddress($this->values['street_address']);
			$contact->setPostCode($this->values['post_code']);
			$contact->setArea($this->values['area']);
			$contact->setCityId($this->values['city_id']);
			$contact->setRegionId($this->values['region_id']);
			$contact->save();

			unset($this->values['work_phone'], $this->values['mobile_phone'], $this->values['street_address'], $this->values['post_code'], $this->values['post_code'], $this->values['area'], $this->values['city_id'], $this->values['region_id']);
		}
	}

}
